package com.xinsoft.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.common.exception.BDException;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.ScmOutsourcingInstockParam;
import com.xinsoft.entity.dto.ScmOutsourcingInstockDTO;
import com.xinsoft.entity.po.ScmOutsourcingInstock;
import com.xinsoft.entity.po.ScmOutsourcingInstockItem;
import com.xinsoft.entity.vo.ScmOutsourcingInstockDetailVo;
import com.xinsoft.entity.vo.ScmOutsourcingInstockPageVo;
import com.xinsoft.mapper.ScmOutsourcingInstockItemMapper;
import com.xinsoft.service.ScmOutsourcingInstockItemService;
import com.xinsoft.service.ScmOutsourcingInstockService;
import com.xinsoft.mapper.ScmOutsourcingInstockMapper;
import com.xinsoft.service.SysDocumentNumService;
import com.xinsoft.utils.TokenUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* @author ZYM
* @description 针对表【scm_outsourcing_instock(委外入库)】的数据库操作Service实现
* @createDate 2023-09-04 14:43:24
*/
@Service
public class ScmOutsourcingInstockServiceImpl extends ServiceImpl<ScmOutsourcingInstockMapper, ScmOutsourcingInstock>
    implements ScmOutsourcingInstockService{
    @Resource
    private ScmOutsourcingInstockItemMapper outsourcingInstockItemMapper;

    @Autowired
    private ScmOutsourcingInstockItemService scmOutsourcingInstockItemService;
    @Autowired
    private SysDocumentNumService sysDocumentNumService;

    @Override
    public IPage<ScmOutsourcingInstockPageVo> findPageListByParam(ScmOutsourcingInstockParam param) {
        Page<ScmOutsourcingInstockPageVo> page = new Page<>(param.getCurrentPage(), param.getPageSize());
        IPage<ScmOutsourcingInstockPageVo> pageList = baseMapper.findPageListByParam(page, param);
        return pageList;
    }

    @Override
    public List<ScmOutsourcingInstockPageVo> findListByParam(ScmOutsourcingInstockParam param) {
        return baseMapper.findPageListByParam(param);
    }

    @Override
    public Integer tempSave(ScmOutsourcingInstockDTO dto) {
        ScmOutsourcingInstock instock = new ScmOutsourcingInstock();
        BeanUtils.copyProperties(dto, instock);
        instock.setAuditStatus(Const.AUDIT_STATUS_WAIT);
        List<ScmOutsourcingInstockItem> itemList = dto.getScmOutsourcingInstockItemList();
        // 保存或新增
        if (Objects.isNull(instock.getId())) {
            // 更新流水号
            sysDocumentNumService.incrementLastSerialByDocumentId(Const.DOCUMENT_ID.OUTSOURCING_INSTOCK_DOCUMENT);
        } else {
            scmOutsourcingInstockItemService.remove(Wrappers.lambdaQuery(ScmOutsourcingInstockItem.class).eq(ScmOutsourcingInstockItem::getInstockId, instock.getId()));
        }
        this.saveOrUpdate(instock);
        // 保存明细
        itemList.forEach(item -> {
            item.setInstockId(instock.getId());
        });
        scmOutsourcingInstockItemService.saveBatch(itemList);
        return instock.getId();
    }

    @Override
    public ScmOutsourcingInstockDetailVo detail(Integer id) {
        ScmOutsourcingInstockDetailVo vo = baseMapper.getInstockById(id);
        vo.setScmOutsourcingInstockItemList(outsourcingInstockItemMapper.getItemListByInstockId(id));
        return vo;
    }

    @Override
    public void del(List<Integer> ids) {
        List<ScmOutsourcingInstock> list = this.list(Wrappers.lambdaQuery(ScmOutsourcingInstock.class)
                .eq(ScmOutsourcingInstock::getAuditStatus, Const.AUDIT_STATUS_NORMAL)
                .in(ScmOutsourcingInstock::getId, ids));
        if (list.size() > 0) {
            throw new BDException("操作失败，已审核的数据不能删除");
        }
        scmOutsourcingInstockItemService.remove(Wrappers.lambdaQuery(ScmOutsourcingInstockItem.class).in(ScmOutsourcingInstockItem::getInstockId, ids));
        this.removeByIds(ids);
    }

    @Override
    public void audit(Integer id) {
        ScmOutsourcingInstock instock = this.getById(id);
        if (Const.AUDIT_STATUS_NORMAL.equals(instock.getAuditStatus())) {
            throw new BDException("该单据已审核，无需重复审核");
        }
        instock.setAuditId(TokenUtil.getUserId());
        instock.setAuditTime(new Date());
        instock.setAuditStatus(Const.AUDIT_STATUS_NORMAL);
        this.updateById(instock);
    }

    @Override
    public void reverseAudit(Integer id) {
        ScmOutsourcingInstock instock = this.getById(id);
        if (Const.AUDIT_STATUS_WAIT.equals(instock.getAuditStatus())) {
            throw new BDException("该单据未审核，无需反审核");
        }
        // TODO 产生下游单据不能反审核
        // int citeCount = baseMapper.getOutsourcingOutstockCiteCount(id);
        // if (citeCount > 0) {
        //     throw new BDException("该单据已产生下游单据，不能反审核");
        // }
        instock.setAuditId(null);
        instock.setAuditTime(null);
        instock.setAuditStatus(Const.AUDIT_STATUS_WAIT);
        this.updateById(instock);
    }
}




